package com.manhanlou.service;

import com.manhanlou.dao.BillDao;
import com.manhanlou.domain.Bill;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wty
 * @date 2022/11/9 14:51
 */
public class BillService {
    private BillDao billDao = new BillDao();

    public int insert(String billId, Integer menuId,
                      Integer nums, Double money, Integer diningTableId,
                      String state) {
        String sqlStr = "insert into bill values(null,?,?,?,?,?,now(),?)";
        int affectedRows = billDao.dml(sqlStr, billId, menuId, nums, money, diningTableId, state);
        return affectedRows;
    }

    public List<Bill> queryBill() {
        List<Bill> list = new ArrayList<>();
        String sqlStr = "SELECT id,menuId,nums,money,diningTableId,billDate,state FROM bill";

        list = billDao.queryMulti(sqlStr, Bill.class);

        return list;
    }

    public int updateBillState(Integer diningTableId, String state) {
        String sqlStr = "UPDATE bill SET state = ? WHERE diningTableId = ? AND state = ?";
        int affectedRows = billDao.dml(sqlStr, state, diningTableId, "未结账");
        return affectedRows;
    }

    /**
     * 判断是否有未支付的账单
     *
     * @param
     * @return boolean
     * @date 2022/11/9 22:11
     * @author wty
     **/
    public boolean checkPayAccount(Integer diningTableId) {
        String sqlStr = "SELECT * FROM bill WHERE diningTableId = ? AND state = ? limit 0,1";
        Bill bill = billDao.querySingle(sqlStr, Bill.class, diningTableId, "未结账");

        return bill != null;
    }
}
